/**注册第二步*/
function GetRequest() {
	
	var url = unescape(location.search);
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
$(function(){
	var cookie = new sweetCookie();
	var advid = cookie.getCookie("advid");
	if(advid==null){
		location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/login/E-mail.html";
	}
	//将advid放在cookie里
	var object = GetRequest();
	var cookie = new sweetCookie();
	cookie.setCookie("registerAdvId",object.advId,1000*60*10);
	//正则验证
	 function companyname(){ //公司名称
	 	 var companyNameVal=$("input[name='companyName']").val();
	 	 var companyName=/^[A-Za-z0-9\u4e00-\u9fa5]{0,30}$/;
	 	 if(companyName.test(companyNameVal)){
	 		 return true;
	 	 }else{
	 		 return false;
	 	 }
	  };
	  $("#companyNameSpan").hide();
	  $("input[name='companyName']").on("blur",function(){
		  var result=companyname();
		  if(!$(this).val()){
			  $("#companyNameSpan").html("*必填").css("right","-34px").show();
		  }else if(result){
			  $("#companyNameSpan").hide();
		  }else{
			  $("#companyNameSpan").html("*由中英文、数字组成的30个以内的字符").css("right","-243px").show();
		  }
	  });
	  function companyaddressname(){ //公司地址
	 	 var companyNameVal=$("input[name='companyAddress']").val();
	 	 var companyName=/^[A-Za-z0-9\u4e00-\u9fa5]{0,30}$/;
	 	 if(companyName.test(companyNameVal)){
	 		 return true;
	 	 }else{
	 		 return false;
	 	 }
	};
	$("#companyAddressSpan").hide();
	$("input[name='companyAddress']").on("blur",function(){
		var result=companyaddressname();
		if(!$(this).val()){
			$("#companyAddressSpan").html("*必填").css("right","-34px").show();
		}else if(result){
			$("#companyAddressSpan").hide();
		}else{
			$("#companyAddressSpan").html("*由中英文、数字组成的30个以内的字符").css("right","-243px").show();
		}
	});
	$("#urlSpan").hide();
	$("input[name='websiteUrl']").on("blur",function(){
		if(!$(this).val()){
			$("#urlSpan").css("right","-34px").show();
		}else{
			$("#urlSpan").hide();
		}
	});
	function mustval(){ //非空验证
		var urlVal=$("input[name='websiteUrl']").val();
	 	var mustVal=/^\S|\S$/;
	 	if(mustVal.test(urlVal)){
	 		return true;
	 	}else{
	 		return false;
	 	}
	};
	
	var valLogo;
	var val;
	var valCode;
	function ImgLoad(){
		if(!valLogo || !val || !valCode){
			return false;
		}else{
			return true;
		}
	}
	$("body").on("mousemove",function(){
		if(mustval() && companyname() && companyaddressname() && ImgLoad() && $("input[name='companyName']").val() && $("input[name='companyAddress']").val() && $("input[name='websiteUrl']").val() ){
			$(".step1SubmitBtn").attr("disabled",false);
			$(".step1SubmitBtn").on("click",function(){
				doregister2();
			})
		}else{
			$(".step1SubmitBtn").attr("disabled",true);
		}
		valLogo=$(".uploadImgLogo img").attr("src");
		val=$(".uploadImg img").attr("src");
		valCode=$(".uploadImgCode img").attr("src");
		if(valLogo){
			$("#btnOne").html("重新上传")
		};
		if(val){
			$("#btnTwo").html("重新上传")
		};
		if(valCode){
			$("#btnThree").html("重新上传")
		}
	})
	var sweetcookie = new sweetCookie();
	    var donkeyNum =  sweetcookie.getCookie("donkeyNum");
	    var name =  sweetcookie.getCookie("name");
	    var advid =  sweetcookie.getCookie("advid");
	    var email =  sweetcookie.getCookie("email");
	$("#donkeyNum").text(donkeyNum);
	$("#donkeyNum2").val(donkeyNum);
	$("#name").val(name);
	$("#advId").val(advid);
	
	
});
function dataURItoBlob(dataURI) {
	var byteString = atob(dataURI.split(',')[1]);
	var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
	var ab = new ArrayBuffer(byteString.length);
	var ia = new Uint8Array(ab);
	for (var i = 0; i < byteString.length; i++) {
		ia[i] = byteString.charCodeAt(i);
	}
	return new Blob([ab], {type: mimeString});
}
var uploadImg={
		changeImg:function(a,b){
			var file = new FileReader(),files = a.files[0];
			var fileName = files.name;
			var suffix="";
			if(files.size>2*1024*1024 ||files.size<2 ){
				 console.log("==========="+files.size)
				 alert("单照图片在2K-2M之间，请您重新选择图片！");
				 return false;
			}
			suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length);
			suffix=suffix.toUpperCase();//jpg、gif、png
			if(suffix!="PNG"&&suffix!="GIF"&&suffix!="JPEG"&&suffix!="JPG"){
				alert("请您上传jpg、gif、png格式的图片！")
				return false;
			}
			file.readAsDataURL(files);
			file.onload = function(event){
				var src = this.result;
				var fd = new FormData();
				var blob = dataURItoBlob(src);
				fd.append('file', blob,fileName);
				$.ajax({
					 url : '/Lvscore_Advertiser_Maven/uploadImgs',
					 type : 'post',
					 data:fd,
					 processData: false, // 不会将 data 参数序列化字符串
					 contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
					 success : function(data) {
						 var data=JSON.parse(data);
						 if(data.status=="1"){
							 $("#logo_url").val(data.data);
							 $("#logo_img").attr("src",data.picServerPrex+data.data);
						 }else{
							 alert("保存失败！");
						 }
					 },
					 error: function(){}
				})
		    }
		} 	
	}

var uploadImg2={
		changeImg:function(a,b){
			var file = new FileReader(),files = a.files[0];
			var fileName = files.name;
			var suffix="";

			
			if(files.size>2*1024*1024 ||files.size<2 ){
				 console.log("==========="+files.size)
				 alert("单照图片在2K-2M之间，请您重新选择图片！");
				 return false;
			}
			suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length);
			suffix=suffix.toUpperCase();//jpg、gif、png
			if(suffix!="PNG"&&suffix!="GIF"&&suffix!="JPEG"&&suffix!="JPG"){
				alert("请您上传jpg、gif、png格式的图片！")
				return false;
			}
			file.readAsDataURL(files);
			file.onload = function(event){
				var src = this.result;
				var fd = new FormData();
				var blob = dataURItoBlob(src);
				fd.append('file', blob,fileName);
				$.ajax({
					 url : '/Lvscore_Advertiser_Maven/uploadImgs',
					 type : 'post',
					 data:fd,
					 processData: false, // 不会将 data 参数序列化字符串
					 contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
					 success : function(data) {
						 var data=JSON.parse(data);
						 if(data.status=="1"){
							 $("#buss_url").val(data.data);
							 $("#buss_img").attr("src",data.picServerPrex+data.data);
						 }else{
							 alert("保存失败！");
						 }
					 },
					 error: function(){}
				})
		    }
		} 	
	}

var uploadImg3={
		changeImg:function(a,b){
			var file = new FileReader(),files = a.files[0];
			var fileName = files.name;
			var suffix="";
			if(files.size>2*1024*1024 ||files.size<2 ){
				 console.log("==========="+files.size)
				 alert("单照图片在2K-2M之间，请您重新选择图片！");
				 return false;
			}
			suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length);
			suffix=suffix.toUpperCase();//jpg、gif、png
			if(suffix!="PNG"&&suffix!="GIF"&&suffix!="JPEG"&&suffix!="JPG"){
				alert("请您上传jpg、gif、png格式的图片！")
				return false;
			}
			file.readAsDataURL(files);
			file.onload = function(event){
				var src = this.result;
				var fd = new FormData();
				var blob = dataURItoBlob(src);
				fd.append('file', blob,fileName);
				$.ajax({
					 url : '/Lvscore_Advertiser_Maven/uploadImgs',
					 type : 'post',
					 data:fd,
					 processData: false, // 不会将 data 参数序列化字符串
					 contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
					 success : function(data) {
						 var data=JSON.parse(data);
						 if(data.status=="1"){
							 $("#org_url").val(data.data);
							 $("#org_img").attr("src",data.picServerPrex+data.data);
						 }else{
							 alert("保存失败！");
						 }
					 },
					 error: function(){}
				})
		  }
	 } 	
}


function doregister2() {
		$.ajax({
			url : "/Lvscore_Advertiser_Maven/registerPageTwo",
			type : "post",
			dataType : "json",
			data : $("#adform2").serialize(),
			success : function(data) {
				
				var result = data.error;
				if(result=="此公司名称已注册"){
					$("#companyNameSpan").css("right","-111px").show();
				}else{
					var registerReturnBean = data.registerReturnBean;
					var donkeyNum=registerReturnBean.donkeyNum;
					var companyName=registerReturnBean.companyName;
					var name=registerReturnBean.name;
					var sweetcookie = new sweetCookie();
	                sweetcookie.delCookie("donkeyNum");
	                sweetcookie.setCookie("donkeyNum", donkeyNum);
	                sweetcookie.delCookie("name");
	                sweetcookie.setCookie("name", name);
	                sweetcookie.delCookie("companyName");
	                sweetcookie.setCookie("companyName",companyName);
					 location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/login/registerStep3.html";
				}
				
			}
		});
}

/*function initUpload() {
new AjaxUpload("upload_logo", {
    action: "/Lvscore_Advertiser_Maven/uploadChaImg",
    name: "card",
    data: {},
    onSubmit: function (file, extension) {
        extension = extension.toUpperCase();
        if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
        	alert("只支持图片文件！");
            return false;
        }
       
        if(extension.size>10*1024*1024 ||extension.size<10 ){
			 console.log("======图片====="+extension.size)
			 alert("单照图片在10K-10M之间，请您重新选择图片！");
			 return false;
		}
    },
    onComplete : function(file,data){
        if(data){
            if(data.status==1){
                $("#logo_url").val(data.data);
                $("#logo_img").attr("src",data.picServerPrex+data.data);
            }
        }else{
            alert("登录失败，请重试！！");
        }
    }
});

new AjaxUpload("upload_business_license", {
    action: "/Lvscore_Advertiser_Maven/uploadChaImg",
    name: "card",
    data: {},
    onSubmit: function (file, extension) {
        extension = extension.toUpperCase();
        if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
        	alert("只支持图片文件！");
            return false;
        }
        if(extension.size>10*1024*1024 ||extension.size<10 ){
			 console.log("======图片====="+extension.size)
			 alert("单照图片在10K-10M之间，请您重新选择图片！");
			 return false;
		}
    },
    onComplete: function (file, result) {
        if (result) {
            if (result.status == 1) {
                $("#buss_url").val(result.data);
                $("#buss_img").attr("src",result.picServerPrex+result.data);

            }
        } else {
            alert("登录失败，请重试！！");
        }
    }
});

new AjaxUpload("upload_organization", {
    action: "/Lvscore_Advertiser_Maven/uploadChaImg",
    name: "card",
    data: {},
    onSubmit: function (file, extension) {
        extension = extension.toUpperCase();
        if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
            alert("只支持图片文件！");
            return false;
        }
        if(extension.size>10*1024*1024 ||extension.size<10 ){
			 console.log("======图片====="+extension.size)
			 alert("单照图片在10K-10M之间，请您重新选择图片！");
			 return false;
		}
    },
    onComplete: function (file, result) {
        if (result) {
            if (result.status == 1) {
                $("#org_url").val(result.data);
                $("#org_img").attr("src",result.picServerPrex+result.data);
            }
        } else {
            alert("登录失败，请重试！！");
        }
    }
});
}

*/
